package com.lanxiao.cloud.modules.file.application;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanxiao.cloud.common.core.enums.StateEnum;
import com.lanxiao.cloud.modules.file.domain.factory.UploadFileFactory;
import com.lanxiao.cloud.modules.file.domain.model.FileConfigure;
import com.lanxiao.cloud.modules.file.infrastructure.repository.FileConfigureMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ：qiaoliang
 * @date ：2021-10-25
 */
@Component
public class UploadCmdManager {

  private final UploadFileFactory uploadFileFactory;
  private final FileConfigureMapper fileConfigureMapper;

  public UploadCmdManager(
      UploadFileFactory uploadFileFactory,
      FileConfigureMapper fileConfigureMapper) {
    this.uploadFileFactory = uploadFileFactory;
    this.fileConfigureMapper = fileConfigureMapper;
  }

  /**
   *  文件上传
   *
   * @author ：qiaoliang
   * @param file : 文件
   * @return java.lang.String
   * @date 2021-10-25 14:23
   */
  public String upload(MultipartFile file) {
    final FileConfigure fileConfigure = fileConfigureMapper.selectOne(
        new LambdaQueryWrapper<FileConfigure>().eq(FileConfigure::getState, StateEnum.NORMAL));
    return uploadFileFactory.handle(fileConfigure.getType()).uploadFile(file, fileConfigure);
  }

}
